//
//  FindBackDetailViewController.m
//  geiTA
//
//  Created by 曲丹阳 on 14-11-22.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FindBackDetailViewController.h"
#import "FindSixModel.h"
#import "FindDetailCell.h"
#import "FindDetailViewController.h"
#import "MJRefresh.h"

@interface FindBackDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation FindBackDetailViewController
- (void)dealloc
{
    [_modelarray release];
    [_data release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self creatTable];
    
    [self setupRefresh];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.data = [NSData data];
        self.modelarray = [NSMutableArray array];
        self.nextpage = [NSMutableDictionary dictionary];
        
    }
    return self;
}

#pragma mark -TableView
- (void)creatTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:UITableViewStylePlain];
    [self.table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    _table.delegate = self;
    _table.dataSource = self;
    
    
    [self.view addSubview:_table];
    [_table release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_modelarray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *pool = @"detailpool";
    FindDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:pool];
    if (!cell) {
        cell = [[FindDetailCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:pool];
    }
    cell.model = [_modelarray objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FindDetailViewController *find = [[FindDetailViewController alloc] init];
    FindSixModel *model = [_modelarray objectAtIndex:indexPath.row];
    
    [find getdetailurlModel:model];//方法传值
    
    [self.navigationController pushViewController:find animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return tableView.frame.size.height / 4.2;
}


#pragma mark -网络获取
- (void)GetConnect:(NSString *)url
{
    NSURL *urlstr = [NSURL URLWithString:url];
    NSURLRequest *request = [NSURLRequest requestWithURL:urlstr];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        self.data = data;
        [self creatJson];
        
    }];
    
    
}

#pragma mark -方法传值
- (void)GetId:(NSString *)Id
{
    self.myid = Id;
    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v1/collections/%@/posts?limit=12&offset=0", _myid];
    [self GetConnect:str];
    
}

#pragma mark -json解析
- (void)creatJson
{
    NSMutableDictionary *bigdic = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];
    NSMutableDictionary *datas = [bigdic objectForKey:@"data"];
    self.nextpage = [datas objectForKey:@"paging"];   // 获取下一页，用于刷新
    NSMutableArray *posts = [datas objectForKey:@"posts"];
    
    for (NSMutableDictionary *smalldic in posts) {
        FindSixModel *model = [[FindSixModel alloc] init];
        [model setValuesForKeysWithDictionary:smalldic];
        [_modelarray addObject:model];
    }
    [_table reloadData];
}

#pragma mark -刷新
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.table addHeaderWithTarget:self action:@selector(headerRereshing)];
    //#warning 自动刷新(一进入程序就下拉刷新)
    [self.table headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.table addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.table.headerPullToRefreshText = @"下拉可以刷新了";
    self.table.headerReleaseToRefreshText = @"松开马上刷新了";
    self.table.headerRefreshingText = @"正在刷新中";
    
    // self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.table.footerReleaseToRefreshText = @"松开马上加载更多数据";
    self.table.footerRefreshingText = @"正在加载中";
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        [self.table reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.table headerEndRefreshing];
    });
}

- (void)footerRereshing
{
    // 判断有无可刷新页面
    if ([self.nextpage objectForKey:@"next"]!= [NSNull null]) {
        [self GetConnect:[self.nextpage objectForKey:@"next"]];
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.table reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.table footerEndRefreshing];
    });
    
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
